//
//  HouseTypeViewController2.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/7.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "HouseTypeViewController2.h"
#import "HouseTypeScrollView1.h"
#import "HouseTypeScrollView2.h"
#import "HouseDetailDataManage.h"
#import "HouseTypeViewModel.h"
#import "DistanceCell.h"
#import "FangYuanCell.h"
#import "FangXingCell.h"
#import "HouseIntroducedCell.h"
#import "disDescModel.h"
#import "AroundSchollViewController.h"
#import "NoDataView.h"
#define SROLLVIEWHEIHGT 1270



@interface HouseTypeViewController2 ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource,houseTypeScrollView1Delegate>

@property(strong,nonatomic) UIScrollView *mainView;

@property (strong, nonatomic) HouseTypeViewModel * model;

@property (strong, nonatomic) HouseDetailDataManage *dataManage;
@property (strong, nonatomic) HouseTypeScrollView1 *scrollView1;
@property (strong, nonatomic) HouseTypeScrollView2 *scrollView2;

@property (assign,nonatomic)NSInteger scrollview2Heihgt;
@property (strong, nonatomic) NoDataView *nodataView;


@end

@implementation HouseTypeViewController2

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"房屋详情";
    
     self.dataManage = [[HouseDetailDataManage alloc]init];
    [self freshData];

    [self initScrollView];
}

#pragma mark 初始化scrollView
-(void)initScrollView
{
    HouseTypeScrollView2 *scrollView =  [HouseTypeScrollView2  housetTypeScrollView2WithXib];
    scrollView.frame = CGRectMake(0, UIScreenWidth, UIScreenWidth, UIScreenHeight);
    scrollView.contentSize = CGSizeMake(UIScreenWidth,self.scrollview2Heihgt);
    scrollView.delegate = self;
    scrollView.tableView.frame = CGRectMake(0, 0, UIScreenWidth, self.scrollview2Heihgt);
    scrollView.tableView.scrollEnabled = NO;
    
    scrollView.tableView.delegate = self;
    scrollView.tableView.dataSource =self;
    scrollView.tableView.allowsSelection = NO;
    scrollView.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.scrollView2 = scrollView;
    
    
    
    HouseTypeScrollView1 *scrollView1 =  [HouseTypeScrollView1  houseTypeScrollView1WithXib];
    scrollView1.frame = CGRectMake(0,0, UIScreenWidth, UIScreenHeight);;
    scrollView1.contentSize = CGSizeMake(UIScreenWidth, SROLLVIEWHEIHGT);
    scrollView1.delegate = self;
    scrollView1.model = self.model;
    scrollView1.delegate1 = self;
    [scrollView1 createMap];
    self.scrollView1 = scrollView1;
    self.view.frame = CGRectMake(0, 0, UIScreenWidth, UIScreenHeight);
    [self.view addSubview:self.scrollView1];
    

//    self.nodataView = [[NoDataView alloc]initWithFrame:self.view.frame];
//    [self.view addSubview:self.nodataView];
//    [self.nodataView setState:nodataCellStateLoading];
}

#pragma mark 把距离表述，筛选到最多只有三条（中国城，学区房，商业中心）
-(void)selectDisdesc
{
     NSInteger i = 0,j = 0,k = 0;
    NSMutableArray *arrM = [NSMutableArray arrayWithArray:self.model.disDescArr];
    if (arrM.count == 0)
    {
        disDescModel *model = [[disDescModel alloc]init];
        model.disDesc = @"暂无数据";
        model.lTypeName = @"暂无数据";
        [self.model.disDescArr addObject:model];
    }
    else
    {
        for (disDescModel *mod in arrM)
        {
            
            if ([mod.lTypeName isEqual:@"中国城房"])
            {
                i ++;
                if (i > 1)
                {
                    [self.model.disDescArr removeObject:mod];
                }
            }else if([mod.lTypeName isEqual:@"学区房"])
            {
                j ++;
                if (j > 1)
                {
                    [self.model.disDescArr removeObject:mod];
                }
                
            }else
            {
                k ++;
                if (k > 1)
                {
                    [self.model.disDescArr removeObject:mod];
                }
                
            }
            
        }

    }
    
}

#pragma mark 获取数据
-(void)freshData
{
    
//    __block typeof(self) bself = self;
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        
//        NSInteger consumerId = 73;
//        NSInteger PremiseshousingId = self.premiseshousingId;
//        ResulteMod *resultMod = [self.dataManage refreshDataWithConsumerId:consumerId WithPremiseshousingId:PremiseshousingId];
//        self.model = [[HouseTypeViewModel alloc]init];
//        self.model = resultMod.dataMod;
//        
//        [self calculatescrollview2Height];
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            if (resultMod==nil) {//网络出错
//                [bself.nodataView setState:nodataCellStateNetError];
//                
//            }else if(resultMod == nil){//没数据
//                [bself.nodataView setState:nodataCellStateNodata];
//            }else{//有数据
//                
//                [bself.nodataView setState:nodataCellStateNorMal];
//            }
//            
////            [bself.tableView reloadData];
//        });
//    });
//}

    
   
    NSInteger consumerId = 73;
    NSInteger PremiseshousingId = self.premiseshousingId;
    ResulteMod *resultMod = [self.dataManage refreshDataWithConsumerId:consumerId WithPremiseshousingId:PremiseshousingId];
    self.model = [[HouseTypeViewModel alloc]init];
    self.model = resultMod.dataMod;
    
    [self calculatescrollview2Height];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark uiscrollview的代理，实现上拉查看图文详情
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{
    CGFloat y= scrollView.contentOffset.y;
    NSInteger h = UIScreenHeight -(SROLLVIEWHEIHGT - y);
    if (h >50)
    {
        if ([scrollView isKindOfClass:[HouseTypeScrollView1 class]])
        {
            [UIView beginAnimations:nil context:nil];
            //动画持续时间
            [UIView setAnimationDuration:1.0];
            
            
            self.scrollView2.frame = CGRectMake(0, 64, UIScreenWidth, UIScreenHeight);
            
            self.scrollView1.frame = CGRectMake(0, -SROLLVIEWHEIHGT, UIScreenWidth, UIScreenHeight);
            
            [self.view addSubview:self.scrollView2];
            
            //提交动画
            [UIView commitAnimations];
            

        }
        
    }
    else if(scrollView.contentOffset.y < -100)
    {
        if ([scrollView isKindOfClass:[HouseTypeScrollView2 class]])
        {
            [UIView beginAnimations:nil context:nil];
            //动画持续时间
            [UIView setAnimationDuration:1.0];
            
            self.scrollView1.frame = CGRectMake(0, 0, UIScreenWidth, UIScreenHeight);
            [self.view addSubview:self.scrollView1];
            self.scrollView2.frame = CGRectMake(0, UIScreenWidth, UIScreenWidth, UIScreenHeight);
            
            
            //提交动画
            [UIView commitAnimations];
        }
        
        
    }
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.row == 0)
    {
        return [HouseIntroducedCell cellWithTableView:tableView WithDetail:self.model.detail WithDetailEnglish:self.model.detailEnglish];
    }else if(indexPath.row == 1)
    {
        [self selectDisdesc];
        
        return [DistanceCell cellWithTable:tableView withDisDescArr:self.model.disDescArr];
    }else if (indexPath.row == 2)
    {
          return [FangXingCell cellWithTableView:tableView WithFangXingPicsArr:self.model.fangxingPicsArr];
    }
    else
    {
        return [FangYuanCell cellWithTableView:tableView WithFangXingPicsArr:self.model.fangyuanPicsArr];
    }

   
    
}

#pragma mark 计算scrollview2的高度
-(void)calculatescrollview2Height
{
    self.scrollview2Heihgt = 0;
     NSInteger height1 = 0,height2 = 0,height3 = 0,height4 = 0;
    height1 = 32+40*self.model.disDescArr.count +10;
    height2 = self.model.fangxingPicsArr.count * ( 577*((UIScreenWidth - 2*8)/524))+ 32 + 10 + 10*(self.model.fangxingPicsArr.count-1);
     height3 = self.model.fangyuanPicsArr.count*(215*(UIScreenWidth/320.0)) + 32+ 10+10*(self.model.fangxingPicsArr.count-1);
    
     height4 = 208;
    
     self.scrollview2Heihgt = height1+height2+height3+height4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    if (indexPath.row == 1)
    {
         ;
        return 32+40*self.model.disDescArr.count +10 ;
    }
    else if(indexPath.row == 2)
    {
        

        return  self.model.fangxingPicsArr.count * ( 577*((UIScreenWidth - 2*8)/524))+ 32 + 10 + 10*(self.model.fangxingPicsArr.count-1);
    }
    else if(indexPath.row == 3)
    {
       
        
        return self.model.fangyuanPicsArr.count*(215*(UIScreenWidth/320.0)) + 32+ 10+10*(self.model.fangxingPicsArr.count-1);
    }
    else
    {
       
        return 208;
    }
    
   }

#pragma mark houseTypeScrollView1Delegate  点击查看周边学校
-(void)houseTypeScrollView1DelegateClickBtn:(HouseTypeViewModel *)model
{
    
}

@end
